//
//  SinaWelComeViewController.swift
//  weibo
//
//  Created by sirius on 16/1/5.
//  Copyright © 2016年 sirius. All rights reserved.
//

import UIKit
import SnapKit

class SinaWelComeViewController: UIViewController {
    //定义一个全局的约束
    var bottomSnapKitConstraint: Constraint?
    var bottomConstraint: NSLayoutConstraint?
    
    override func loadView() {
         view = UIImageView(image: UIImage(named: "ad_background"))
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        setupUI()
        // Do any additional setup after loading the view.
    }

    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        //更新约束
//        bottomConstraint?.constant = -(UIScreen.mainScreen().bounds.height - 150)
        bottomSnapKitConstraint?.updateOffset(-(UIScreen.mainScreen().bounds.height - 150))
        UIView.animateWithDuration(2, delay: 0, usingSpringWithDamping: 0.4, initialSpringVelocity: 0, options: [], animations: { () -> Void in
             //强制刷新
            self.view.layoutIfNeeded()
            }) { (_) -> Void in
                NSNotificationCenter.defaultCenter().postNotificationName(kNotificationEnter,object: nil)
        }
    }
    private func setupUI(){
        
        view.addSubview(photoImageView)
        view.addSubview(nameLabel)
//        photoImageView.translatesAutoresizingMaskIntoConstraints = false
//        nameLabel.translatesAutoresizingMaskIntoConstraints = false
//        view.addConstraint(NSLayoutConstraint(item: photoImageView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
//        bottomConstraint =  NSLayoutConstraint(item: photoImageView, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: -150)
//        view.addConstraint(bottomConstraint!)
//        view.addConstraint(NSLayoutConstraint(item: nameLabel, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
//        view.addConstraint(NSLayoutConstraint(item: nameLabel, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: photoImageView, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 10))
//    
        //设置头像的约束
        photoImageView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(view.snp_centerX)
            bottomSnapKitConstraint = make.bottom.equalTo(view.snp_bottom).offset(-150).constraint
        }
        nameLabel.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(view.snp_centerX)
            make.top.equalTo(view.snp_bottom).offset(10)
        }
    }
    lazy var photoImageView: UIImageView = {
    let img = UIImageView()
    img.image = UIImage(named: "avatar_default_big")
    img.layer.cornerRadius = 170 / 2.0 / 2.0
    img.layer.masksToBounds = true
    img.layer.borderWidth = 3
    img.layer.borderColor = UIColor.blueColor().CGColor
    return img
    }()
    lazy var nameLabel: UILabel = {
        let label = UILabel()
        label.text = "欢迎归来"
        label.font = UIFont.systemFontOfSize(16)
        label.textColor = UIColor.blueColor()
        return label
    }()
    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
